package com.kcs.dc.report.model.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import com.kcs.common.utils.Constant;
import com.kcs.common.utils.LogWriter;

public class userprofileDAO {
	
	public boolean deleteUser(String user_id, String userIdSession, Connection conn) throws SQLException {
		
		int num_record = 0;
		CallableStatement proc = null;
		
		try {
			proc = conn.prepareCall("{ call update_user_authorized_proc(?, ?, ?, ?, ?, ?, ?, ?) }");
			proc.setString(1, user_id);
			proc.setString(2, "");  // user name	
			proc.setString(3, "N"); // user status
			proc.setString(4, "");  // expire date
			proc.setString(5, "");  // menu name
			proc.setString(6, "");  // menu status
			proc.setString(7, userIdSession);
			proc.registerOutParameter(8, Types.INTEGER);
			
			proc.execute();   
			conn.commit();
			
			num_record = proc.getInt(8);			
			LogWriter.debug("User ID :" + user_id + ", Return delete success : " + num_record, Constant.LOGGER_APPLICATION);	
					
			
		} catch (Exception e) {
			LogWriter.error("1.1 userprofileDAO [Method : deleteUser call Store update_user_authorized_proc] : " + e.getMessage(), Constant.LOGGER_APPLICATION);
			throw new SQLException();
		} finally {
			if (proc != null) {
				try {
					proc.close();
				} catch (Exception e) {
					LogWriter.error("1.2 userprofileDAO [Method : deleteUser call Store update_user_authorized_proc] : " + e.getMessage(), Constant.LOGGER_APPLICATION);
					throw new SQLException();
				}
			}		
		}
		return true; 
	}	
	
	public boolean updateUser(String user_id, String userIdSession,
			String user_name, String expire_date, String menu_name,
			String menu_status, Connection conn) throws SQLException {
		
		int num_record = 0;
		CallableStatement proc = null;
		
		try {
			proc = conn.prepareCall("{ call update_user_authorized_proc(?, ?, ?, ?, ?, ?, ?, ?) }");
			proc.setString(1, user_id);
			proc.setString(2, user_name); 
			proc.setString(3, "");  //user_status 
			proc.setString(4, expire_date); 			 
			proc.setString(5, menu_name); 
			proc.setString(6, menu_status); 
			proc.setString(7, userIdSession);
			proc.registerOutParameter(8, Types.INTEGER);
			
			proc.execute();   
			conn.commit();
			
			num_record = proc.getInt(8);			
			LogWriter.debug("User ID :" + user_id + ", Return update success : " + num_record, Constant.LOGGER_APPLICATION);	
					
			
		} catch (Exception e) {
			LogWriter.error("1.1 userprofileDAO [Method : updateUser call Store update_user_authorized_proc] : " + e.getMessage(), Constant.LOGGER_APPLICATION);
			throw new SQLException();
		} finally {
			if (proc != null) {
				try {
					proc.close();
				} catch (Exception e) {
					LogWriter.error("1.2 userprofileDAO [Method : updateUser call Store edit_user_authorized_proc] : " + e.getMessage(), Constant.LOGGER_APPLICATION);
					throw new SQLException();
				}
			}		
		}
		return true; 
	}	
	
}
